package com.qfedu.wfx1.controller;

import com.qfedu.wfx1.po.WxbGood;
import com.qfedu.wfx1.po.WxbGoodType;
import com.qfedu.wfx1.service.IGoodsService;
import com.qfedu.wfx1.vo.JsonResultVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/product")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;

    @RequestMapping("/list")
    public String queryGoodsType(Model model){
        List<WxbGoodType> typeList = goodsService.queryGoodsType();
        model.addAttribute("list",typeList);
        return "goods-add";
    }

    @RequestMapping("/save")
    @ResponseBody
    public JsonResultVO saveGood(@RequestBody WxbGood wxbGood){
        JsonResultVO jsonResultVO = new JsonResultVO();
        wxbGood.setCustomerId("69609206");
        try {
            goodsService.save(wxbGood);
            jsonResultVO.setCode(1);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultVO.setCode(0);
        }
        return jsonResultVO;
    }

    @RequestMapping("/login")
    @ResponseBody
    public JsonResultVO login(String username,String password){
        JsonResultVO jsonResultVO = new JsonResultVO();
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            SecurityUtils.getSubject().login(token);
            jsonResultVO.setCode(1);
        }catch (Exception e){
            e.printStackTrace();
            jsonResultVO.setCode(0);
        }
        return jsonResultVO;
    }
    @RequestMapping("/find")
    public String queryGoods(Model model){
        try {
            List<WxbGood> goodList = goodsService.queryGoods();
            model.addAttribute("goodList",goodList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "goods-list";
    }
}
